<?PHP
  $current_page = "user.php";
  $page_type = "A";
  require_once 'header.php';
?>
	<div id="page">
	  <div id="page-padding">
        <!-- start content -->	    
	    <div id="content">
	      <div id="content-padding">
            <h1>User Management Main</h1>
            <?PHP
              if (!empty($_GET["update_success"]))
                $update_success = "&nbsp<font color=\"#CC0000\">{$_GET["update_success"]}</font>";
              else
                $update_success = '';

              if (!empty($_GET["delete_success"]))
                $delete_success = "&nbsp<font color=\"#CC0000\">{$_GET["delete_success"]}</font>";
              else
                $delete_success = '';
  
              if (!empty($_GET["reset_success"]))
                $reset_success = "&nbsp<font color=\"#CC0000\">{$_GET["reset_success"]}</font>";
              else
                $reset_success = '';

              if (!empty($_GET["delete_error"]))
                $delete_error = "&nbsp<font color=\"#CC0000\">{$_GET["delete_error"]}</font>";
              else
                $delete_error = '';
  
              if (!empty($_GET["reset_error"]))
                $reset_error = "&nbsp<font color=\"#CC0000\">{$_GET["reset_error"]}</font>";
              else
                $reset_error = '';

              // Generate the query section
              echo("<form method=\"post\" action=\"user.php\">
                    Username: <input type=\"text\" size=\"20\" maxlength=\"30\" name=\"input_user_id\"> 
                    First Name: <input type=\"text\" size=\"20\" maxlength=\"30\" name=\"input_fname\">
                    Last Name: <input type=\"text\" size=\"20\" maxlength=\"30\" name=\"input_lname\"><br><br>
                    DOB(DD-MON-YY): <input type=\"text\" size=\"20\" maxlength=\"9\" name=\"input_dob\">
					Gender:
                    <select name=\"input_gender\">
                      <option value=\"\">All</option>
                      <option value=\"m\">Male</option>
                      <option value=\"f\">Female</option></select><br><br>
                    Street: <input type=\"text\" size=\"20\" maxlength=\"50\" name=\"input_street\"> 
                    City: <input type=\"text\" size=\"20\" maxlength=\"50\" name=\"input_city\">
                    State: <input type=\"text\" size=\"20\" maxlength=\"50\" name=\"input_state\">
                    Zip: <input type=\"text\" size=\"20\" maxlength=\"12\" name=\"input_zip\"><br><br>");

              // Dropdown menu for account type
              echo("Account Type:
                    <select name=\"input_account_type\">
                      <option value=\"\">All</option>
                      <option value=\"A\">Administrator</option>
                      <option value=\"S\">Stakeholder</option>
                      <option value=\"V\">Volunteer</option></select>
					Promotion Request:
					<select name=\"input_promo\">
                      <option value=\"\">All</option>
                      <option value=\"y\">Yes</option>
                      <option value=\"n\">No</option></select>");
  
              if (!empty($_GET["query_error"]))
                $msg = "<font color=\"#CC0000\"><b>&nbsp{$_GET["query_error"]}</b></font>";
              else
                $msg = "";	

              echo(" <input type=\"submit\" value=\"Search\">$msg$update_success$reset_success$delete_success$delete_error$reset_error
                    </form><br><br><br><br><br><br>");
  
              // Interpret the query requirements
              $input_user_id = "";
              $input_fname = "";
              $input_lname = "";
              $input_dob = "";
              $input_street = "";
              $input_city = "";
              $input_state = "";
              $input_zip = "";
			  $input_gender = "";
			  $input_promo = "";
			  $input_account_type = "";

              if (isset($_POST["input_user_id"])) 
                $input_user_id = $_POST["input_user_id"];
              if (isset($_POST["input_fname"])) 
                $input_fname = $_POST["input_fname"];
              if (isset($_POST["input_lname"])) 
                $input_lname = $_POST["input_lname"];
              if (isset($_POST["input_dob"])) 
                $input_dob = $_POST["input_dob"];
              if (isset($_POST["input_street"])) 
                $input_street = $_POST["input_street"];
              if (isset($_POST["input_city"])) 
                $input_city = $_POST["input_city"];
              if (isset($_POST["input_state"])) 
                $input_state = $_POST["input_state"];
              if (isset($_POST["input_zip"])) 
                $input_zip = $_POST["input_zip"];
			  if (isset($_POST["input_gender"])) 
                $input_gender = $_POST["input_gender"];
			  if (isset($_POST["input_promo"])) 
                $input_promo = $_POST["input_promo"];
			  if (isset($_POST["input_account_type"])) 
                $input_account_type = $_POST["input_account_type"];

              $whereClause = " 1=1 ";

              // Add inputted user id to where clause
              if (isset($input_user_id) and trim($input_user_id) != "") { 
                $whereClause .= " and UserId='$input_user_id'"; 
              }
              // Add inputted first name to where clause
              if (isset($input_fname) and trim($input_fname) != "") { 
                $whereClause .= " and Fname like '%$input_fname%'"; 
              }
              // Add inputted last name to where clause
              if (isset($input_lname) and trim($input_lname) != "") { 
                $whereClause .= " and Lname like '%$input_lname%'"; 
              }
              // Add inputted dob to where clause
              if (isset($input_dob) and trim($input_dob) != "") { 
                $whereClause .= " and DOB like '$input_dob'"; 
              }
              // Add inputted street to where clause
              if (isset($input_street) and trim($input_street) != "") { 
                $whereClause .= " and Street like '%$input_street%'";
              }
              // Add inputted city to where clause
              if (isset($input_city) and trim($input_city) != "") { 
                $whereClause .= " and City like '%$input_city%'"; 
              }
              // Add inputted state to where clause
              if (isset($input_state) and trim($input_state) != "") { 
                $whereClause .= " and State like '%$input_state%'"; 
              }
              // Add inputted zipcode to where clause
              if (isset($input_zip) and trim($input_zip) != "") { 
                $whereClause .= " and Zip like '%$input_zip%'"; 
              }
			  // Add inputted gender to where clause
              if (isset($input_gender) and $input_gender != "") { 
                  $whereClause .= " and Gender='$input_gender'"; 
              }
			  // Add inputted promo to where clause
              if (isset($input_promo) and $input_promo != "") { 
                  $whereClause .= " and Promo_Request='$input_promo'"; 
              }
			  // Add inputted account type to where clause
              if (isset($input_account_type) and $input_account_type != "") { 
                if ($input_account_type == "A")
                  $whereClause .= " and AFlag='y' and SFlag='n' and VFlag='n'"; 
                else if ($input_account_type == "S")
                  $whereClause .= " and AFlag ='n' and SFlag='y' and VFlag='n'"; 
                else if ($input_account_type == "V")
                  $whereClause .= " and AFlag='n' and SFlag='n' and VFlag='y'";
              }

              // Form the query statement array and run it
              $sql = "select * from USER_ACCT where $whereClause";
              $result_array = execute_sql_in_oracle($sql);
              $result = $result_array["flag"];
              $cursor = $result_array["cursor"];

              if (!$result) {
                $query_error = "User account query failed!";
                header("Location:user.php?query_error=$query_error");
              }
              else {
                // Display the query results
                echo "<table border=1>";
                echo "<tr> <th>Username</th> <th>Account Type</th> <th>First Name</th> <th>Last Name</th> <th>DOB</th> <th>Gender</th>
                      <th>Address</th> <th>Promotion</th> <th>Update</th> <th>Delete</th> <th>Reset Password</th></tr>";

                // Fetch the result from the cursor one by one
                while ($values = oci_fetch_array($cursor)) {
                  $user_id = $values[0];
				  $password = $values[1];
                  $fname = $values[2];
                  $lname = $values[3];
	              $dob = $values[4];
                  $street = $values[5];
                  $city = $values[6];
                  $state = $values[7];
                  $zip = $values[8];
				  $email = $values[9];
				  $phone = $values[10];
				  $gender = $values[11];
				  $promo = $values[12];
				  $aflag = $values[13];
                  $sflag = $values[14];
	              $vflag = $values[15];
				  
				  // Account type
                  if ($aflag == 'y')
                    $account_type = "Administrator";
                  else if ($sflag == 'y')
                    $account_type = "Stakeholder";
                  else if ($vflag == 'y') 
                    $account_type = "Volunteer";
	              else
	                $account_type = "";
				  
				  // Gender
				  if ($gender == 'm')
                    $sex = "Male";
                  else if ($gender == 'f') 
                    $sex = "Female";
	              else
	                $sex = "";
					
				  // Promotion Request
				  if ($promo == 'y')
                    $promo_req = "Yes";
                  else if ($promo == 'n') 
                    $promo_req = "No";
	              else
	                $promo_req = "";

                  $address = formatAddress($street, $city, $state, $zip);
  
                  echo("<tr>" . 
                       "<td>$user_id</td> <td>$account_type</td> <td>$fname</td> <td>$lname</td> <td>$dob</td> <td>$sex</td> <td>$address</td> <td>$promo_req</td>".
                       "<td> <A HREF=\"user_update.php?user_id=$user_id\">Update</A> </td> ".
                       "<td> <A HREF=\"user_delete.php?user_id=$user_id\">Delete</A> </td> ".
                       "<td> <A HREF=\"reset_password.php?user_id=$user_id\">Reset Password</A> </td> ".
                       "</tr>");
                }
                oci_free_statement($cursor);
                echo "</table>";
              }
            ?>
	      </div>
		</div>
	  <!-- end content --></div>
	  <div id="footer">
	    <div id="footer-pad">
	      <div class="line"></div>
		  <!-- footer and copyright notice -->
	      <p>Design by <a href="http://www.steves-templates.com">Steve's free website templates</a>.</p>
		  <!-- end footer and copyright notice -->
	    </div>
	  </div>
	</div>
  </body>
</html>